package org.schedule.controller;


import org.schedule.entity.Schedule;
import org.schedule.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/schedule")
public class ScheduleController {


    @Autowired
    private ScheduleService scheduleService;

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public boolean deleteSalary(@PathVariable Integer id) {
        return scheduleService.deleteSchedule(id);
    }

    // 新增日程信息
    @PostMapping("/save")
    @ResponseBody
    public boolean saveSchedule(@RequestBody Schedule schedule) {
        boolean result = scheduleService.addSchedule(schedule);
        if (!result) {
            throw new RuntimeException("用户不存在！");
        }
        return result;
    }

    // 更新日程信息
    @PostMapping("/update")
    @ResponseBody
    public boolean updateSchedule(@RequestBody  Schedule schedule) {
        return scheduleService.updateSchedule(schedule);
    }

    // 根据关键词搜索
    @GetMapping("/search")
    @ResponseBody
    public List<Schedule> searchSalary(@RequestParam Integer userId, @RequestParam String content) {
        return scheduleService.searchSchedulesByContent(userId, content);
    }

    //根据id查询日程
    @GetMapping("/{id}")
    @ResponseBody
    public Schedule  getScheduleById(@PathVariable Integer id) {
        return scheduleService.getScheduleById(id);
    }


    @GetMapping("/getSchedulesByUserId")
    @ResponseBody
    public List<Schedule> getSchedulesByUserId(@RequestParam Integer userId) {
        return scheduleService.getSchedulesByUserId(userId);
    }

}
